# File: cdf.py

# """ Start debugging
import sys
import os.path
this_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, this_dir)
# """ End debugging

import cdflib as clib
import os.path

class CDF(object):
    """
    This class defines a CDF object. 

    A CDF object is only bound at most one CDF data file. It supports the
    following operations:
    1. Open a CDF file.
    2. Close a CDF file.
    3. Get a list of all global attributes.
    4. Retrieve a single global attribute.
    5. Retrieve all global attributes.
    6. Get a list of all variables.
    7. Get a list of variable attributes given one variable.
    8. Retrieve a single variable attribute given the attribute name and the
       variable.
    9. Retrieve all variable attributes given one variable.
    10. Retrieve data given one variable.
    11. Retrieve the entire data of the CDF file.
    """

    def __init__(self):
        """
        This function does the follwoing things:
        1. Remove .cdf extension in filepath_in if it is there.
        3. Set self._filepath and self._opened = False.
        4. Open the CDF file. 
        5. Set self._opened = True
        """
        # Initialize attributes.
        self._opened = False
        self._filepath = None

    def __del__(self):
        if self._opened:
            self.close()


#++++++++++++++++++++++++++++++
    def open(self, filepath_in):
        # Remove any file extension.
        if self._opened is True:
            raise IOError, 'There is an opened CDF file linked to ' + \
                    'this CDF object. Close it before opening another one.'
        else:
            filepath, ext = os.path.splitext(filepath_in)
            if ext == '.cdf':
                self._filepath = filepath_in
                cdfname = filepath
            else:
                self._filepath = filepath_in + '.cdf'
                cdfname = filepath_in

        clib.open(cdfname)
        self._opened = True


#++++++++++++++++++++++++++++++
    def close(self):
        if self._opened:
            cdfname = os.path.splitext(self._filepath)[0]
            clib.close(cdfname)
            self._opened = False

#++++++++++++++++++++++++++++++
    def fileinfo(self):
        """Retrieve file information.

        """
        if self._opened: return clib.fileinfo()

    def filepath(self):
        """Retrieve file path of the current CDF file.

        """
        return self._filepath

    def datastats(self):
        """Retrieve data information.

        """
        if self._opened: return clib.datastats()

    def varlist(self):
        """Retrieve a list of all variables.

        """
        if self._opened: return clib.varlist()

    def data(self, varName):
        """Retrieve data of the given variable name.

        """
        if self._opened: return clib.data(varName)

    def gAttrList(self):
        """Retrieve the full list of global attributes.

        """
        if self._opened: return clib.gAttrList()

    def vAttrList(self):
        """Retrieve the full list of global attributes.

        """
        if self._opened: return clib.vAttrList()



# #++++++++++++++++++++++++++++++
#     def globalMetadata(self):
#         if self.globalMetadata is None and self._opened:
#             self.globalMetadata = clib.globalMetadata()

